package swd.work.dataview_wizard;

import com.pub.database.*;
import javax.servlet.http.*;
import com.pub.web.*;

public class Action {
   QueryBean gqbnDAO=null;
   public String action(javax.servlet.jsp.PageContext pageContext){
      HttpServletRequest request=(HttpServletRequest)pageContext.getRequest();
      HttpServletResponse response=(HttpServletResponse)pageContext.getResponse();

      View view=(View)request.getAttribute("view");
      (new WebFunction()).setPortal_to_session(request); //set session from portal
      if (request.getSession().getAttribute("u_id")==null) {
         try{
            request.getRequestDispatcher((new StringBuffer("login.jsp")).insert(0,"/").toString()).forward(request,response);
            return "login";
         }
         catch(Exception le){
            view.setOnloadScript("can not redirect to login page...please contact IT");
            return "";
         }
      }
      gqbnDAO=new QueryBean(view.getDBAlias(),view.getDBDebug(),view.getSourceCharset(),view.getTargetCharset());

      Business business=new Business(gqbnDAO);

      String lstrResult=null;

      try{
         if((view.getPrgAction()!=null && (view.getPrgAction().equals("query") || view.getPrgAction().equals("pre_app")))){
            lstrResult=business.selectTableDataToFld(view);

            if(lstrResult!=null && !lstrResult.equals("")){
               view.setOnloadScript("alert('" + lstrResult + "');");
            }
         }
         if(view.getPrgAction()!=null && view.getPrgAction().equals("add")){
            // view.setdataview_seqno(FormatFunction.addZeros(String.valueOf(business.getDataViewMaxSeqno(view.getprg_no())+1),4));
             if(business.checkNotNullFld(view).equals("")){
               if(business.checkPrgnoExists(view)){
                 lstrResult = business.exeInsSQL(view);
                 if (lstrResult == null || lstrResult.equals("")) {
                     business.insertDefaultProperty(view);
                     //view.setOnloadScript("alert('新增成功!!!')");
                     //新增成功就導頁到dataview_wizard_detail
                     view.setOnloadScript(
                             "this.location.href='/SWD/pages/work/dataview_wizard_detail.jsp?prg_no=" +
                             view.getprg_no() + "&dataview_seqno=" +
                             view.getdataview_seqno() + "'");
                 } else {
                     view.setOnloadScript("alert('" + lstrResult + "')");
                 }
             }//check prg_no
             else{view.setOnloadScript("alert('"+view.getprg_no()+"不存在wkf_sys_prg！請先透過阿強二號新增該支程式之名稱！')");}
             }//check not null
         }

         return "";
      }
      catch(Exception e){
         view.setOnloadScript("alert('Program error please contact IT.');");
         return "";
      }
      finally{
          try{business.setdb_alias_options(view);}catch(Exception e){System.out.println("can not query db_alias in action.java");}
         try{
            if(gqbnDAO!=null){
               gqbnDAO.close();
            }
         }
         catch(Exception e){
            System.out.println("can not close querybean in Action.java");
         }
      }
   }
}
